package com.ruoyi.ucenter.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.model.auth.AuthContextHolder;
import com.ruoyi.model.user.UserCheck;
import com.ruoyi.model.user.UserInfo;
import com.ruoyi.ucenter.mapper.UserCheckMapper;
import com.ruoyi.ucenter.service.IUserCheckService;
import com.ruoyi.ucenter.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 申请组织Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-15
 */
@Service
public class UserCheckServiceImpl implements IUserCheckService {

    @Autowired
    private UserCheckMapper userCheckMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public UserCheck selectUserCheckByUserId(String userId) {
        return userCheckMapper.selectUserCheckByUserId(userId);
    }

    /**
     * 查询申请组织
     *
     * @param id 申请组织主键
     * @return 申请组织
     */
    @Override
    public UserCheck selectUserCheckById(String id) {
        return userCheckMapper.selectUserCheckById(id);
    }

    /**
     * 查询申请组织列表
     *
     * @param userCheck 申请组织
     * @return 申请组织
     */
    @Override
    public List<UserCheck> selectUserCheckList(UserCheck userCheck) {
        return userCheckMapper.selectUserCheckList(userCheck);
    }

    /**
     * 新增申请组织
     *
     * @param userCheck 申请组织
     * @return 结果
     */
    @Override
    public int insertUserCheck(UserCheck userCheck) {
        userCheck.setCreateTime(DateUtils.getNowDate());
        return userCheckMapper.insertUserCheck(userCheck);
    }

    /**
     * 修改申请组织
     *
     * @param userCheck 申请组织
     * @return 结果
     */
    @Override
    public int updateUserCheck(UserCheck userCheck) {
        userCheck.setUpdateTime(DateUtils.getNowDate());
        return userCheckMapper.updateUserCheck(userCheck);
    }


    /**
     * 审核申请组织
     *
     * @param userCheck 申请组织
     * @return 结果
     */
    @Override
    @Transactional
    public int auditUserCheck(UserCheck userCheck) {
        userCheck.setAuditTime(DateUtils.getNowDate());
        userCheckMapper.auditUserCheck(userCheck);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userCheck.getUserId());
        redisTemplate.delete("loginUser:" + userCheck.getUserId());
        userInfo.setIsOrganization(userCheck.getStatus().equals("2") ? "0" : "1");
        boolean result = userInfoService.updateById(userInfo);
        if (result) {
            //修改成功 重新存入redis
            redisTemplate.opsForValue().set("loginUser:" + userCheck.getUserId(), userInfoService.getById(userCheck.getUserId()));
        }
        return result ? 1 : 0;
    }

    /**
     * 批量删除申请组织
     *
     * @param ids 需要删除的申请组织主键
     * @return 结果
     */
    @Override
    public int deleteUserCheckByIds(String[] ids) {
        String userId = AuthContextHolder.getUserId();
        return userCheckMapper.deleteUserCheckByIds(ids, userId);
    }

    /**
     * 删除申请组织信息
     *
     * @param id 申请组织主键
     * @return 结果
     */
    @Override
    public int deleteUserCheckById(String id) {
        String userId = AuthContextHolder.getUserId();
        return userCheckMapper.deleteUserCheckById(id, userId);
    }

    @Override
    public UserCheck userSelectUserCheckById(String id) {
        String userId = AuthContextHolder.getUserId();
        return userCheckMapper.userSelectUserCheckById(id, userId);
    }
}
